
#include "esp32_spiffs.h"

#include <FS.h>
#include "SPIFFS.h"

void ESP32Spiffs::init()
{
    if (!SPIFFS.begin())
    {
        while (1)
            yield();
    }
}

void ESP32Spiffs::read(const char * path)
{
    if (!SPIFFS.exists(path))
    {
        Serial.println(F("Jpeg file not found"));
    }

    File file = SPIFFS.open(path, "r");
    if (!file || file.isDirectory())
    {
        Serial.println("- failed to open file for reading");
        return;
    }

    while (file.available())
    {
        Serial.write(file.read());
    }
    file.close();
}

void ESP32Spiffs::test(const char * path)
{
    static uint8_t buf[512];
    File file = SPIFFS.open(path);
    if (file && !file.isDirectory())
    {
        size_t len = file.size();
        while (len)
        {
            size_t toRead = len;
            if (toRead > 512)
                toRead = 512;
            file.read(buf, toRead);
            len -= toRead;
        }
        file.close();
    }
}
